﻿using NServiceKit.OrmLite;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace GameSave.Entity
{
    class SqliteManager
    {
        /// <summary>
        /// 连接生成器
        /// </summary>
        private OrmLiteConnectionFactory connFactory;

        private IDbConnection curConn;

        /// <summary>
        /// 创建一个连接，如果已经存在连接会关闭当前连接
        /// </summary>
        /// <param name="dbAccessPath"></param>
        public IDbConnection CreateConnection(string dbAccessPath)
        {
            if(curConn != null)
            {
                curConn.Close();
            }
            if (connFactory == null)
            {
                connFactory = new OrmLiteConnectionFactory(dbAccessPath, SqliteDialect.Provider);
            }
            curConn = connFactory.OpenDbConnection();
            return curConn;
        }

        public void SaveEntity(BaseEntity entity)
        {
            curConn.Update<BaseEntity>(entity);
        }

        
    }
    

}
